package com.verify.repository.base;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.verify.data.domain.BaseEntity;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Map;

/**
 * @author Wang Chao
 * @date 2024-04-07 18:17
 * @since 2.0
 */
public class BaseRepository<M extends BaseMapper<T>, T extends BaseEntity> extends ServiceImpl<M, T> implements IService<T> {
    protected LambdaQueryWrapper<T> baseQuery() {
        return Wrappers.lambdaQuery();
    }

    protected LambdaUpdateWrapper<T> baseUpdateQuery() {
        return Wrappers.lambdaUpdate();
    }

    protected LambdaUpdateWrapper<T> baseDeleteQuery() {
        return baseUpdateQuery().set(T::getIsDelete, LocalDateTime.now().toString());
    }

    public LambdaQueryWrapper<T> baseOrderBy(LambdaQueryWrapper<T> wrapper) {
        return wrapper;
    }

    @Override
    public boolean removeById(Serializable id) {
        return update(baseDeleteQuery().eq(T::getId, id));
    }

    @Override
    public boolean removeByMap(Map<String, Object> columnMap) {
        UpdateWrapper<T> wrapper = new UpdateWrapper<>();
        wrapper.set("is_delete", LocalDateTime.now().toString());
        columnMap.forEach(wrapper::eq);
        return update(wrapper);
    }

    @Override
    public boolean remove(Wrapper<T> wrapper) {
        return update(wrapper);
    }

    public boolean removeByIds(Collection<?> ids) {
        return update(baseDeleteQuery().in(T::getId, ids));
    }
}
